<?php namespace module\base\controller;

use houdunwang\request\Request;
use module\base\model\BaseContent;
use module\HdController;
use system\model\Category;
use system\model\Keywords;
use tests\Base;

class Wx extends HdController
{
    public function index()
    {
        //?m=base&action=controller/wx/index
        //echo url('wx.index');


        $base_controller = BaseContent::get()->toArray();
//		p(current($base_controller));die;

        //------  多表关联  -----
        $join = Db::table('keywords')
            ->join("base_content", 'keywords.kid', '=', 'base_content.id')
            ->paginate( 10 );
//        p($join);

        //分配数据
//		$Keywords = Keywords::get()->toArray();
        View::with('join', $join);
        return $this->template();
    }

    //添加回复
    public function post()
    {
        //1.接收get参数id，baseContent回复表的自增id，是编辑当前的自增id
        $id = Request::get('id');
        //p($id);
        //2.get参数有id说明是编辑操作  ？ ： 如果没有get id参数说明是新增则实例化新的模型
        $baseModel = BaseContent::find($id) ?: new BaseContent();
        //p($baseModel);
        if (IS_POST) {
            $post = Request::post();
            //p($post);
            //添加回复表
            //var_dump($baseModel['id']);//NULL
            $baseModel->save($post);
            //var_dump($baseModel['id']);//2
            //添加关键词表
            $post['module_id'] = $baseModel['id'];
            $this->saveKeywords($post);
//			$post['module_id'] = $baseModel['id'];
//			$post['module'] = Request::get('m');
//			(new Keywords())->save($post);
            return $this->setRedirect(url('wx.index'))->success('操作成功');

        }
        $this->assignKeywords($id);
        View::with('baseModel', $baseModel);
        return $this->template();
    }

    public function del()
    {
        //1.获取get参数 id
        $id = Request::get('id');
        //p($id);die;
        //2.BaseContent::find($id)接收$id 进行删除回复内容
        $model = BaseContent::find($id);
        //3.执行删除
        $model->destory();
        //4.删除关键词
        $this->delKeywords($id);
        //成功提示
        return $this->setRedirect(url('wx.index'))->success('操作成功');
    }
}